// pages/coming/coming.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    signNum:0,
    location_disabled: false,
    coming:"签到",
    location_disabledOne: false,
    comingOne: "签到",
    signNumOne: 0,
    location_disabledTwo: false,
    comingTwo: "签到",
    signNumTwo: 0,
    location_disabledThree: false,
    comingThree: "签到",
    signNumThree: 0,
    "name":'',
    ismorning: false,
    isevening: false,
    isdormitory: false,
    isactivity:false,
    latitude:'',
    longitude:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function (options) {
    var _this = this
    var UserInfo = app.globalData.UserInfo
    var username = UserInfo.username
    // console.log(UserInfo)
    console.log(username)
    wx.request({
      url: 'https://nottoday.club/index.php/api/Attendance/getAttendanceForStudent',
      data: {
        "stunumber": username,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",  //post
        'cookie': wx.getStorageSync("sessionid")
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log(res.data)
        if(res.statusCode == 200) {
          for (var i = 0; i < res.data.data.length; i ++){
            if (res.data.data[i].type == "morning") {
              var ismorning = (res.data.data[i].type == "morning")
              break
            }
          }
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].type == "evening") {
              var isevening = (res.data.data[i].type == "evening")
              break
            }
          }
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].type == "dormitory") {
              var isdormitory = (res.data.data[i].type == "dormitory")
              break
            } 
          }
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].type == "activity") {
              var isactivity = (res.data.data[i].type == "activity")
              break
            }
            //  else {
            //   var isactivity = (res.data.data[i].activity == 1)
            // }
          }
          // var ismorning = (res.data.data[0].morning == 1)
          // var isevening = (res.data.data[0].evening == 1)
          // var isdormitory = (res.data.data[0].dormitory == 1) 
          // var isactivity = (res.data.data[0].activity == 1) 
          if (ismorning == true) {
            _this.setData({
            coming:"已签",
            location_disabled: ismorning
            })    
          }
          if (isevening == true) {
            _this.setData({
              comingOne: "已签",
              location_disabledOne: isevening
            })
          }
          if (isdormitory == true) {
            _this.setData({
              comingTwo: "已签",
              location_disabledTwo: isdormitory
            })
          }
          if (isactivity == true) {
            _this.setData({
              comingThree: "已签",
              location_disabledThree: isactivity
            })
          }
        }     
      },
      fail: function(res) {},
      complete: function(res) {},
    })
    // if(new Date().getHours() == 0) {
    //   this.setData ({
    //     location_disabled:false,
    //   }) 
    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 早自习签到
  locationCheck() {
    var _this = this
    var UserInfo = app.globalData.UserInfo
    var account = UserInfo.username
    var name = UserInfo.curName
    wx.getLocation({
      type: 'wgs84',
      altitude: true,
      success: function (res) {
        console.log(res) //当前经纬度已知
        _this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        var reqData = {
          stunumber: account,
          stuname: name,
          type: "morning",
          lat: _this.data.latitude,
          lng: _this.data.longitude
        }
        console.log(reqData)
        wx.request({
          url: 'https://nottoday.club/index.php/api/Attendance/newAttendance',
          data: {
            stunumber: account,
            stuname: name,
            type: "morning",
            lat: _this.data.latitude,
            lng: _this.data.longitude
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded",
            'cookie': wx.getStorageSync("sessionid")
          },
          method: 'POST',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            console.log(1)
            console.log(res.data)
            if (res.data.status == 0) {
              wx.showToast({
                title: '尚未发起该考勤，无法签到',
                icon: 'none',
                duration: 2000
              })
            } else {
              if (res.data.status == 1){
                console.log(3)
                _this.setData({
                  location_disabled: true,
                  coming: "已签"
                })
                wx.showToast({
                  title: '签到成功',
                  icon: 'success'
                })
              } else{
                wx.showToast({
                  title: '超出签到时间或范围，无法签到',
                  icon: 'none',
                  duration: 2000
                })
              }
            }   
          },
          fail: function (res) { },
          complete: function (res) { },
        });
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    // _this.getLocation(_this)   
  },

  // 晚自习签到
  locationCheckOne() {
    var _this = this
    var UserInfo = app.globalData.UserInfo
    var account = UserInfo.username
    var name = UserInfo.curName
    wx.getLocation({
      type: 'wgs84',
      altitude: true,
      success: function (res) {
        console.log(res) //当前经纬度已知
        _this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        var reqData = {
          stunumber: account,
          stuname: name,
          type: "evening",
          lat: _this.data.latitude,
          lng: _this.data.longitude
        }
        console.log(reqData)
        wx.request({
          url: 'https://nottoday.club/index.php/api/Attendance/newAttendance',
          data: {
            stunumber: account,
            stuname: name,
            type: "evening",
            lat: _this.data.latitude,
            lng: _this.data.longitude
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded",
            'cookie': wx.getStorageSync("sessionid")
          },
          method: 'POST',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            console.log(res.data)
            console.log(2)
            // console.log(res.data.status)
            if (res.data.status == 0) {
              wx.showToast({
                title: '尚未发起该考勤，无法签到',
                icon: 'none',
                duration: 2000
              })
            } else{
              if (res.data.status == 1) {
                _this.setData({
                  location_disabledOne: true,
                  comingOne: "已签",
                })
                wx.showToast({
                  title: '签到成功',
                  icon: 'success'
                })
              }else{
                wx.showToast({
                  title: '超出签到时间或范围，无法签到',
                  icon: 'none',
                  duration: 2000
                })
              }
            } 
          },
          fail: function (res) { },
          complete: function (res) { },
        });
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  // 寝室晚点名签到
  locationCheckTwo: function () {
    var _this = this
    var UserInfo = app.globalData.UserInfo
    var account = UserInfo.username
    var name = UserInfo.curName
    wx.getLocation({
      type: 'wgs84',
      altitude: true,
      success: function (res) {
        console.log(res) //当前经纬度已知
        _this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        var reqData = {
          stunumber: account,
          stuname: name,
          type: "dormitory",
          lat: _this.data.latitude,
          lng: _this.data.longitude
        }
        console.log(reqData)
        wx.request({
          url: 'https://nottoday.club/index.php/api/Attendance/newAttendance',
          data: {
            stunumber: account,
            stuname: name,
            type: "dormitory",
            lat: _this.data.latitude,
            lng: _this.data.longitude
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded",
            'cookie': wx.getStorageSync("sessionid")
          },
          method: 'POST',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            console.log(res.data)
            console.log(2)
            // console.log(res.data.status)
            if (res.data.status == 0) {
              wx.showToast({
                title: '尚未发起该考勤，无法签到',
                icon: 'none',
                duration: 2000
              })
            } else {
              if (res.data.status == 1) {
                _this.setData({
                  location_disabledTwo: true,
                  comingTwo: "已签"
                })
                wx.showToast({
                  title: '签到成功',
                  icon: 'success'
                })
              } else{
                wx.showToast({
                  title: '超出签到时间或范围，无法签到',
                  icon: 'none',
                  duration: 2000
                })
              }
            }  
          },
          fail: function (res) { },
          complete: function (res) { },
        });
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  locationCheckThree: function () {
    var _this = this
    var UserInfo = app.globalData.UserInfo
    var account = UserInfo.username
    var name = UserInfo.curName
    wx.getLocation({
      type: 'wgs84',
      altitude: true,
      success: function (res) {
        console.log(res) //当前经纬度已知
        _this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        var reqData = {
          stunumber: account,
          stuname: name,
          type: "activity",
          lat: _this.data.latitude,
          lng: _this.data.longitude
        }
        console.log(reqData)
        wx.request({
          url: 'https://nottoday.club/index.php/api/Attendance/newAttendance',
          data: {
            stunumber: account,
            stuname: name,
            type: "activity",
            lat: _this.data.latitude,
            lng: _this.data.longitude
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded",
            'cookie': wx.getStorageSync("sessionid")
          },
          method: 'POST',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            console.log(res.data)
            if (res.data.status == 0) {
              wx.showToast({
                title: '尚未发起该考勤，无法签到',
                icon: 'none',
                duration: 2000
              })
            } else{
              if (res.data.status == 1) {
                _this.setData({
                  location_disabledThree: true,
                  comingThree: "已签"
                })
                wx.showToast({
                  title: '签到成功',
                  icon: 'success'
                })
              } else{
                wx.showToast({
                  title: '超出签到时间或范围，无法签到',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          },
          fail: function (res) { },
          complete: function (res) { },
        });
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }


})